package com.fileFormatConvert;

import java.io.*;

public class Run {

    public static void main(String[] args) {
        try {
            // Image paths (extracted as variables)
            String inputImagePath = "C:\\Users\\lenovo\\Desktop\\213\\1.png";
            String outputImagePath = "output_image.jpg";
            String outputImageFromBytesPath = "output_image_from_bytes.jpg";
            String outputImageFromFilePath = "output_image_from_file.jpg";
            File file = new File(inputImagePath);
            ImageFormat imageFormat = ImageFormat.JPEG;
            OutputStream outputStream = ImageConverter.convertToTargetFormatAndGetStream(file, imageFormat);
            boolean b = FileUtils.saveFile(outputStream, "C:\\Users\\lenovo\\Desktop\\213\\", "3" +"."+ imageFormat.getFormatName());
            System.out.println(b);


//            // 从输入流转换并返回字节数组
//            InputStream inputStream = Files.newInputStream(Paths.get(inputImagePath));
//            byte[] imageByteArray = ImageConverter.convertToTargetFormatAndGetByteArray(inputStream, targetFormat);
//            System.out.println("Image byte array length: " + imageByteArray.length);
//
//            // 从字节数组转换并返回字节数组
//            byte[] imageData = Files.readAllBytes(Paths.get(inputImagePath));
//            byte[] imageByteArrayFromBytes = ImageConverter.convertToTargetFormatAndGetByteArray(imageData, targetFormat);
//            System.out.println("Image byte array from bytes length: " + imageByteArrayFromBytes.length);
//
//            // 从文件转换并返回字节数组
//            File imageFile = new File(inputImagePath);
//            byte[] imageByteArrayFromFile = ImageConverter.convertToTargetFormatAndGetByteArray(imageFile, targetFormat);
//            System.out.println("Image byte array from file length: " + imageByteArrayFromFile.length);
//
//            // 从输入流转换并返回输出流（保存到文件）
//            OutputStream outputStream = ImageConverter.convertToTargetFormatAndGetStream(inputStream, targetFormat);
//            try (FileOutputStream fileOutputStream = new FileOutputStream(outputImagePath)) {
//                ((ByteArrayOutputStream) outputStream).writeTo(fileOutputStream);
//            }
//
//            // 从字节数组转换并返回输出流（保存到文件）
//            OutputStream outputStreamFromBytes = ImageConverter.convertToTargetFormatAndGetStream(imageData, targetFormat);
//            try (FileOutputStream fileOutputStreamFromBytes = new FileOutputStream(outputImageFromBytesPath)) {
//                ((ByteArrayOutputStream) outputStreamFromBytes).writeTo(fileOutputStreamFromBytes);
//            }
//
//            // 从文件转换并返回输出流（保存到文件）
//            OutputStream outputStreamFromFile =ImageConverter. convertToTargetFormatAndGetStream(imageFile, targetFormat);
//            try (FileOutputStream fileOutputStreamFromFile = new FileOutputStream(outputImageFromFilePath)) {
//                ((ByteArrayOutputStream) outputStreamFromFile).writeTo(fileOutputStreamFromFile);
//            }

        } catch (ImageProcessingException e) {
            e.printStackTrace();
        }
    }
}
